import Vue from 'vue'
import VueRouter from 'vue-router'
import IndexView from '@/views/index/indexView.vue'
import DetailView from '@/views/detail/detailView.vue'
import SearchView from '@/views/search/searchView.vue'
import UserView from '@/views/user/userView.vue'
import ShopView from '@/views/shop/shopView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: IndexView,
  },
  {
    path: '/detail/:courseId', // 定义动态路由参数
    name: 'CourseDetail',
    component: DetailView,
    props: true, // 将路由参数作为组件的 props
  },
  {
    path:'/search',
    name:'search',
    component:SearchView
  },
  {
    path:'/user',
    name:'user',
    component:UserView
  },
  {
    path:'/shop',
    name:'shop',
    component:ShopView
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from,next) => {
  console.log(to);
  console.log(from);
  if(to.name == 'shop'){
    console.log(1);
    if(localStorage.getItem('vuex')){
      console.log(2);
      next()
    }else{
      console.log(3);
      next({
        name:'search',
        query:{
          redirect:to.search
        }
      })
    }
  }else{
    console.log(4);
    next()
  }
})

export default router
